//
//  DKLoginModel.swift
//  Sina
//
//  Created by 赵柏清 on 16/7/31.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit

class DKLoginModel: NSObject, NSCoding {
/// 用户授权的唯一票据
    var access_token : String?
/// access_token的生命周期,单位是秒
    var expires_in : NSTimeInterval = 0 {
        didSet{
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
/// 把过去时效的秒数转换成时间
    var expires_date : NSDate?
/// 即将作废,用expires_in代替
    var remind_in : NSTimeInterval = 0
/// 授权用户的UID，本字段只是为了方便开发者，减少一次user/show接口调用而返回的，第三方应用不能用此字段作为用户登录状态的识别，只有access_token才是用户授权的唯一票据。
    var uid : String?
/// 用户昵称
    var screen_name : String?
/// profile_image_url	string	用户头像地址（中图），50×50像素
    var profile_image_url : String?
    
    init(dict : [String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    // 此方法必须要实现不然会报错,接档
    required init?(coder aDecoder: NSCoder) {
        
        self.access_token = aDecoder.decodeObjectForKey("access_token") as? String
        self.expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        self.uid = aDecoder.decodeObjectForKey("uid") as? String
        self.screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        self.profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
        
        super.init()
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        printLog("\(key) : \(value)")
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")
    }
    
    static let sandBoxPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!
    
    // 储存模型对象
    func saveData(fileName: String){
        // 获得完整的模型之后进行归档保存
        // 获取沙盒路径
        
        // 拼接文件名
        let filePath = (DKLoginModel.sandBoxPath as NSString).stringByAppendingPathComponent(fileName)
        
        
        NSKeyedArchiver .archiveRootObject(self, toFile: filePath)
        
    }
    
    // 读取模型对象
    class func readModel() -> DKLoginModel? {
        let LoginM = NSKeyedUnarchiver.unarchiveObjectWithFile((DKLoginModel.sandBoxPath as NSString).stringByAppendingPathComponent("userInfo")) as? DKLoginModel
        
        // 在此要判断一下access_token的时效,如果没有过时则返回,过时了则返回nil
        // 其实应该和微博的服务器进行判断
        
        printLog("\(LoginM?.expires_date)")
        
        guard let outTime = LoginM?.expires_date else {
            return nil
        }
        
        if outTime.compare(NSDate()) == .OrderedDescending {
            
            return LoginM
        } else {
            return nil
        }
        
        
        
    }

}
